package com.czxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czxy.rbac.SysPermission;
import com.czxy.service.SysPermissionService;
import com.czxy.util.JWTUtil;
import com.czxy.vo.BaseResult;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/perm")
public class PermissionController {

    @Autowired
    private SysPermissionService permissionService;

    @GetMapping("/all")
    public BaseResult findAllPermissions(){

        List<SysPermission> list = permissionService.findAll();
        //3 返回结果
        return BaseResult.ok(list);

    }

    //解析token获取
    @GetMapping("/userPerm")
    public BaseResult findPermByUserId(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        Claims claims = JWTUtil.parseToken(token, "teacher");
//        Object teaId = claims.get("teaId");
        Integer userId = 4;
        Set<SysPermission> permSet = permissionService.findPermByUserIdAndParentId(userId);
        return BaseResult.ok(permSet);
    }


}
